package com.bridgeQQ.client.util
{
	import com.bridgeQQ.client.model.BridgeModel;
	import com.bridgeQQ.client.vo.FriendType;
	import com.bridgeQQ.client.vo.User;
	import com.bridgeQQ.client.vo.UserPossess;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.utils.ObjectUtil;
	
	public class ModelUtil
	{
		public static function getFriendType(possess:UserPossess, typeID:Number):FriendType
		{
			var types:ArrayCollection = possess.friendTypes;
			var type:FriendType;
			for(var i:int = types.length - 1; i >= 0; i--)
			{
				var temp:FriendType = types.getItemAt(i) as FriendType;
				if(temp != null && temp.id == typeID)
				{
					type = temp;
					break;
				}
			}
			return type;
		}
		
		public static function getFriendTypeByUser(possess:UserPossess, email:String):FriendType
		{
			var types:ArrayCollection = possess.friendTypes;
			var result:FriendType;
			var user:User;
			for(var i:int = types.length - 1; i >= 0; i--)
			{
				var type:FriendType = types.getItemAt(i) as FriendType;
				if(type == null)
				{
					continue;
				}
				var users:ArrayCollection = type.friends;
				for(var k:int = users.length - 1; k >= 0; k--)
				{
					if(users.getItemAt(k).email == email)
					{
						result = type;
						break;
					}
				}
				if(result != null)
				{
					break;
				}
			}
			return result;
		}
		
		public static function getFriend(possess:UserPossess, propertyName:String, value:Object):User
		{
			if(possess == null)
			{
				return null;
			}
			var types:ArrayCollection = possess.friendTypes;
			var type:FriendType;
			var user:User;
			for(var i:int = types.length - 1; i >= 0; i--)
			{
				type = types.getItemAt(i) as FriendType;
				if(type == null)
				{
					continue;
				}
				var users:ArrayCollection = type.friends;
				for(var k:int = users.length - 1; k >= 0; k--)
				{
					if(users.getItemAt(k)[propertyName] == value)
					{
						user = users.getItemAt(k) as User;
						break;
					}
				}
				if(user != null)
				{
					break;
				}
			}
			return user;
		}
		
		public static function findFriendAtType(friend:String, ft:FriendType):int
		{
			var friends:ArrayCollection = ft.friends;
			for(var i:int = friends.length - 1; i >= 0; i--)
			{
				var result:Object = friends.getItemAt(i);
				if(result != null && result.email == friend)
				{
					return i;
				}
			}
			return -1;
		}
		
		public static function sortFriendTypes(possess:UserPossess, sortFriend:Boolean = false):void
		{
			if(possess == null)
			{
				return;
			}
			var types:ArrayCollection = possess.friendTypes;
			if(types == null || types.length == 0)
			{
				return;
			}
			var sort:Sort = new Sort();
			sort.fields = [new SortField()];
			types.sort = sort;
			types.refresh();
			if(!sortFriend)
			{
				return;
			}
			for(var i:int = types.length - 1; i >= 0; i--)
			{
				sortFriends(types.getItemAt(i) as FriendType);
			}
		}
		
		public static function sortFriends(ft:FriendType):void
		{
			var friends:ArrayCollection = ft.friends;
			var sort:Sort = new Sort();
			sort.compareFunction = friendCompare;
			friends.sort = sort;
			friends.refresh();
		}
		
		private static function friendCompare(a:Object, b:Object, fields:Array = null):int
	    {
	        return ObjectUtil.stringCompare(a["peerID"],  b["peerID"], false);
	    }
	    
	    public static function getAllOnlineFriends(propertyName:String = null):Array
	    {
	    	var up:UserPossess = BridgeModel.instance.possess;
	    	var types:ArrayCollection = up.friendTypes;
	    	var length:int = types.length;
	    	var array:Array = [];
			for(var i:int = 0; i < length; i++)
			{
				var type:FriendType = types.getItemAt(i) as FriendType;
				var users:ArrayCollection = type.friends;
				var userLength:int = users.length;
				for(var k:int = 0; k < userLength; k++)
				{
					var user:User = User(users.getItemAt(i));
					if(user.peerID)
					{
						var result:Object = propertyName == null ? user : user[propertyName];
						array.push(result);
					}
				}
			}
			return array;
	    }
	    
	    public static function getUserFromPool(name:String):User
	    {
	    	var pool:ArrayCollection = BridgeModel.instance.userPool;
			for(var i:int = pool.length - 1; i >= 0; i--)
			{
				var result:Object = pool.getItemAt(i);
				if(result != null && result.email == name)
				{
					return result as User;
				}
			}
			return null;
	    }
	}
}